﻿using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Linq;



    class Login
    {


        public void login(object client)
        {
            Console.WriteLine("\r\nFrom Login Server: Client Connected");//debug
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            //request username and password from client
byte[] request_credz = {
	0x00, 0x05, 0x08, 0x00, 0x00, 0x05, 0x5B, 0x00, 0x00, 0x05, 0x5F, 0x00,
	0x0D, 0x23, 0x01, 0x00, 0x20, 0x74, 0x28, 0x58, 0x56, 0x23, 0x33, 0x21,
	0x3C, 0x31, 0x5F, 0x31, 0x64, 0x6B, 0x52, 0x6F, 0x58, 0x37, 0x77, 0x55,
	0x51, 0x4A, 0x56, 0x67, 0x77, 0x49, 0x59, 0x36, 0x4A, 0x26, 0x34, 0x6B,
	0x63
};
clientStream.Write(request_credz, 0, 49);



            //read username and password
            //username is sent in plain text
            StringBuilder usernameNpass = read(clientStream);



            //format username and password
            string[] split = usernameNpass.ToString().Split(' ');
            string username = split[0].Substring(12, split[0].Length - 12);
            string password = split[1].Substring(0, 32);
            Console.WriteLine("Username: " + username);//debug
            Console.WriteLine("Password Hash: " + password);//debug


            //sending auth successfull packet until i crack that hash
            //sends back Y for "yes authenticated' and users handle
            byte[] auth_success = {
	0x00, 0x59, 0x1C, 0x00, 0x00, 0x0A, 0x67, 0x72, 0x61, 0x63, 0x69, 0x65,
	0x6A, 0x6A, 0x31, 0x33, 0x02, 0xD7, 0xC4, 0x8C, 0x02, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7A, 0x01, 0x3A, 0x00,
	0x34, 0x00, 0x13, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x03, 0x00, 0x00,
	0x00, 0x13, 0x89, 0x03, 0x00, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00,
	0x00, 0x00, 0x19, 0x03, 0x00, 0x00, 0x00, 0x0F, 0xA3, 0x05, 0x00, 0x00,
	0x00, 0x00, 0x0C, 0x05, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x03, 0x00, 0x01,
	0x00, 0x00, 0x12, 0x05, 0x00, 0x00, 0x00, 0x00, 0x25, 0x03, 0x00, 0x00,
	0x00, 0x00, 0x05, 0x03, 0x00, 0x00, 0x00, 0x00, 0x18, 0x03, 0x00, 0x00,
	0x00, 0x27, 0x11, 0x03, 0x00, 0x00, 0x00, 0x0F, 0xA2, 0x05, 0x00, 0x00,
	0x00, 0x00, 0x0B, 0x03, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x03, 0x00, 0x00,
	0x00, 0x1B, 0x59, 0x03, 0x00, 0x01, 0x00, 0x00, 0x11, 0x03, 0x00, 0x00,
	0x00, 0x00, 0x24, 0x05, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00,
	0x00, 0x00, 0x17, 0x03, 0x00, 0x00, 0x00, 0x0F, 0xA1, 0x03, 0x00, 0x01,
	0x00, 0x00, 0x0A, 0x05, 0x00, 0x00, 0x00, 0x00, 0x1D, 0x03, 0x00, 0x00,
	0x00, 0x03, 0xE9, 0x03, 0x00, 0x01, 0x00, 0x00, 0x23, 0x03, 0x00, 0x00,
	0x00, 0x17, 0x72, 0x03, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00,
	0x00, 0x00, 0x16, 0x07, 0x00, 0x00, 0x00, 0x00, 0x29, 0x03, 0x00, 0x01,
	0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x05, 0x00, 0x00,
	0x00, 0x23, 0x29, 0x03, 0x00, 0x00, 0x00, 0x0F, 0xA6, 0x05, 0x00, 0x00,
	0x00, 0x00, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x17, 0x71, 0x03, 0x00, 0x00,
	0x00, 0x00, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, 0x15, 0x03, 0x00, 0x00,
	0x00, 0x00, 0x28, 0x03, 0x00, 0x01, 0x00, 0x00, 0x08, 0x03, 0x00, 0x01,
	0x00, 0x00, 0x1B, 0x03, 0x00, 0x00, 0x00, 0x0B, 0xB9, 0x03, 0x00, 0x00,
	0x00, 0x0F, 0xA5, 0x03, 0x00, 0x01, 0x00, 0x00, 0x0E, 0x05, 0x00, 0x00,
	0x00, 0x00, 0x21, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x00, 0x00,
	0x00, 0x00, 0x14, 0x03, 0x00, 0x00, 0x00, 0x00, 0x07, 0x03, 0x00, 0x00,
	0x00, 0x00, 0x1A, 0x03, 0x00, 0x00, 0x00, 0x0F, 0xA4, 0x05, 0x00, 0x00,
	0x00, 0x00, 0x0D, 0x03, 0x00, 0x00, 0x00, 0x00, 0x20, 0x03, 0x00, 0x00,
	0x00
};
clientStream.Write(auth_success, 0, 349);
 //--------------------------------------------




            //unknown- payload len 5 - sent from client after selecting serve
            //must indicate server selected in some way
            StringBuilder serverSelected = read(clientStream);



            //got ya im sending the ip of the selected server
            //in our case only 1 server available

            byte[] rawData = {
	0x00, 0xA9, 0x38, 0x00, 0x29, 0x00, 0x0F, 0x31, 0x38, 0x34, 0x2E, 0x39,
	0x31, 0x2E, 0x31, 0x37, 0x2E, 0x33, 0x32, 0x15, 0xB3, 0x01, 0x00, 0x20,
	0x20, 0x20, 0x20, 0x31, 0x63, 0x38, 0x63, 0x31, 0x31, 0x63, 0x37, 0x63,
	0x61, 0x36, 0x65, 0x64, 0x30, 0x33, 0x61, 0x35, 0x37, 0x39, 0x35, 0x66,
	0x62, 0x35, 0x35, 0x66, 0x39, 0x63, 0x35, 0x66, 0x34, 0x31, 0x38
};
clientStream.Write(rawData, 0, rawData.Length);

            
         
        }




        private byte[] StringToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
        }
        private StringBuilder read(NetworkStream clientStream)
        {
            byte[] myReadBuffer = new byte[1024];
            StringBuilder myCompleteMessage = new StringBuilder();
            int numberOfBytesRead = 0;
            do
            {
                numberOfBytesRead = clientStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

            }
            while (clientStream.DataAvailable);
            return myCompleteMessage;

        }

    }

